package com.jie.server.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jie.server.modules.system.entity.Dict;
import com.jie.server.modules.system.entity.DictData;
import com.jie.server.modules.system.mapper.DictDataMapper;
import com.jie.server.modules.system.mapper.DictMapper;
import com.jie.server.modules.system.service.IDictService;
import com.jie.server.utils.DictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: 字典表Service业务层处理
 * @author: xiaojie
 * @since: 2024/12/13 0013
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {

    @Autowired
    private DictMapper dictMapper;
    @Autowired
    private DictDataMapper dictDataMapper;

    /**
     * 查询字典表
     * @param dictId 字典表主键
     * @return 字典表
     */
    @Override
    public Dict selectDictByDictId(Long dictId) {
        return dictMapper.selectDictByDictId(dictId);
    }

    /**
     * 查询字典表列表
     * @param dict 字典表
     * @return 字典表集合
     */
    @Override
    public List<Dict> selectDictList(Dict dict) {
        return dictMapper.selectDictList(dict);
    }

    /**
     * 新增字典表
     * @param dict 字典表
     * @return 结果
     */
    @Override
    public boolean create(Dict dict) {
        return save(dict);
    }

    /**
     * 修改字典表
     * @param dict 字典表
     * @return 结果
     */
    @Override
    public boolean update(Dict dict) {
        return updateById(dict);
    }

    /**
     * 批量删除字典表
     * @param dictIds 需要删除的字典表主键
     * @return 结果
     */
    @Override
    public int deleteDictByDictIds(Long[] dictIds) {
        return dictMapper.deleteBatchIds(Arrays.asList(dictIds));
    }

    /**
     * 删除字典表信息
     * @param dictId 字典表主键
     * @return 结果
     */
    @Override
    public int deleteDictByDictId(Long dictId) {
        return dictMapper.deleteById(dictId);
    }

    @Override
    public List<Dict> selectDictTypeAll() {
        List<Dict> list = dictMapper.selectList(new LambdaQueryWrapper<Dict>());
        return list;
    }

    /**
     * 加载字典缓存数据
     */
    @Override
    public void loadingDictCache() {
        DictData dictData = new DictData();
        dictData.setStatus(0);
        // 按字典编码分组
        Map<String, List<DictData>> dictDataMap = dictDataMapper.selectDictDataList(dictData).stream().collect(Collectors.groupingBy(DictData::getDictType));
        for (Map.Entry<String, List<DictData>> entry : dictDataMap.entrySet()) {
            DictUtils.setDictCache(entry.getKey(), entry.getValue().stream().sorted(Comparator.comparing(DictData::getSort)).collect(Collectors.toList()));
        }
    }

    /**
     * 清空字典缓存数据
     */
    @Override
    public void clearDictCache() {
        DictUtils.clearDictCache();
    }

    /**
     * 重置字典缓存数据
     */
    @Override
    public void resetDictCache() {
        clearDictCache();
        loadingDictCache();
    }

}
